import db from './db';
import utils from './utils';
import globalData from './globalData';
import statvfs from '@ohos.file.statvfs';
import imgClient from '../apis/imgClient';

export default class current {
  private static _instance: current = null;

  public static get instance(): current {
    if (this._instance == null)
      this._instance = new current();
    return this._instance;
  }

  private constructor() {
    this.loadMs();
    this.loadInfo();
    this.loadSetting();
  }

  public loadMs(): Promise<boolean> {
    return new Promise((resolve) => {
      let packName = globalData.abilityContext.applicationInfo.name;
      let path = `${globalData.abilityContext.filesDir}/${packName}/download`;
      statvfs.getFreeSize(path).then((res) => {
        this._total = res;
        resolve(true);
      }).catch(() => resolve(false));
    });
  }

  public loadSetting(): Promise<void> {
    return new Promise((resolve) => {
      db.instance.query('network_dl').then((res) => {
        this._networkDl = res as boolean;
      });
      db.instance.query('notify_enable').then((res) => {
        this._notifyEnable = res as boolean;
        resolve();
      });
    });
  }

  public loadInfo(): Promise<void> {
    return new Promise((resolve) => {
      db.instance.query('user_id').then((value) => {
        this._mid = value.toString();
      });
      db.instance.query('login_cookie').then(async (value) => {
        this._cookie = value.toString();
        await imgClient.updateCookie();
      });
      db.instance.query('access_key').then((value) => {
        this._accessKey = value.toString();
      });
      db.instance.query('user_name').then((value) => {
        this.user_name = value.toString();
      });
      db.instance.query('buv_id').then((value) => {
        if (utils.isEmpty(value)) {
          value = utils.buvId();
          db.instance.update('buv_id', value);
        }
        this._buvId = value.toString();
      });
      db.instance.query('user_face').then((value) => {
        this.user_face = value.toString();
        resolve();
      });
    });
  }

  public get cookie() {
    return this._cookie;
  }

  private _cookie: string;

  public get login(): boolean {
    return !utils.isEmpty(this._mid);
  }

  private _total: number = 0;

  public get total() {
    return this._total;
  }

  private _networkDl: boolean = false;

  public get networkDl() {
    return this._networkDl;
  }

  private _notifyEnable: boolean = false;

  public get notifyEnable() {
    return this._notifyEnable;
  }

  private _mid: string = '';

  public get mid() {
    return this._mid;
  }

  private user_face: string = '';

  public get userFace() {
    return this.user_face;
  }

  private user_name: string = '';

  public get userName() {
    return this.user_name;
  }

  private _accessKey: string = '';

  public get accessKey() {
    return this._accessKey;
  }

  private _buvId: string = '';

  public get buvId() {
    return this._buvId;
  }
}